package com.sy.sevice.impl;

import com.sy.constant.BusinessConstant;
import com.sy.constant.RegexConstant;
import com.sy.dto.JsonResult;
import com.sy.entity.Homework;
import com.sy.entity.Notice;
import com.sy.mapper.AdminMapper;
import com.sy.sevice.AdminService;
import com.sy.utils.CommonUtils;
import com.sy.vo.GetNoticeByConditions;
import com.sy.vo.ModifyAdminVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author csd
 * @version 1.0
 * @date 2020/9/14 0011 11:26
 * @usage 管理员类相关的Service实现类
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 获取最新的5条公告在首页进行显示
     *
     * @param getNoticeByConditions 检索条件
     * @return JsonResult对象（notice实体类）
     */
    @Override
    public JsonResult getNoticesByConditions(GetNoticeByConditions getNoticeByConditions) {
        List<Notice> noticesOrderByCtime = adminMapper.getNoticesByConditions(getNoticeByConditions);
        if (null == noticesOrderByCtime || noticesOrderByCtime.size() == 0) {
            return JsonResult.buildFailure(404, "获取公告失败！");
        }
        return JsonResult.buildSuccess(200, noticesOrderByCtime);
    }

    /**
     * 点击某条公告的标题 通过地址栏带入的公告id 获取具体的公告信息
     *
     * @param id 公告id
     * @return
     */
    @Override
    public JsonResult getNoticeByNoticeId(Integer id) {
        Notice noticeByNoticeId = adminMapper.getNoticeByNoticeId(id);
        return JsonResult.buildSuccess(200, noticeByNoticeId);
    }

    /**
     * 上传作业：向数据库添加新增的作业内容
     *
     * @param homework
     * @return
     */
    @Override
    public JsonResult addHomework(Homework homework) {
        boolean success = adminMapper.addHomework(homework);
        if (success) {
            return JsonResult.buildSuccess(200);
        } else {
            return JsonResult.buildFailure(404, "上传作业失败！");
        }

    }
    /**
     * @author JianBo-Shneg
     * @version 1.0
     * @date 2020/9/14 16:24
     * 修改管理员个人信息
     * @param modifyAdminVo 管理员信息携带对象
     * @return 修改成功的管理员信息
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult modifyAdmin(ModifyAdminVo modifyAdminVo) {

        String username = modifyAdminVo.getUsername();
        String oldPwd = modifyAdminVo.getOldPwd();
        String password = modifyAdminVo.getPassword();
        String newPwdConfirm = modifyAdminVo.getNewPwdConfirm();

        //TODO:获得当前登入管理员的id
        modifyAdminVo.setId(1);

        //用户名
        //判断
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400, "用户名不能为空");
        }
        if (!CommonUtils.validateRegex(username, RegexConstant.USER_NAME_REGEX)) {
            return JsonResult.buildFailure(400, "用户名不合法");
        }
        //当前密码
        //获得当前管理员的密码
        if (StringUtils.isBlank(oldPwd)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        //TODO:根据管理员id查询密码

        String pwdByAdminId = adminMapper.getPwdByAdminId(1);

        if (!oldPwd.equals(pwdByAdminId)) {
            return JsonResult.buildFailure(400, "输入的密码不对");
        }
        //新密码
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        if (!password.equals(newPwdConfirm)) {
            return JsonResult.buildFailure(400, "两次输入的密码不一致");
        }
        if (!CommonUtils.validateRegex(password, RegexConstant.STRONG_PWD_REGEX)) {
            return JsonResult.buildFailure(400, "密码不是强密码");
        }
        //将修改后的信息更新到数据库
        boolean isSuccess = adminMapper.updateAdmin(modifyAdminVo);
        if (isSuccess) {
            return JsonResult.buildSuccess(200, modifyAdminVo);
        }
        return JsonResult.buildFailure(400, "修改个人信息失败");

    }

}
